import { BadRequestException, Injectable } from '@nestjs/common';
import * as Redis from 'ioredis';
import config from '../../config';

const { redisDb, redis: { host, port, password } } = config;


@Injectable()
export class RedisService {

  private clients: { [key: string]: Redis.Redis } = {};

  public get(db: string) {
    if (!redisDb[db]) {
      throw new BadRequestException('invalid db');
    }
    if (!this.clients[db]) {
      this.clients[db] = new Redis({ host, port, password, db: redisDb[db] });
    }
    return this.clients[db];
  }

}
